package com.fan.chat.controller;

import com.fan.chat.controller.request.SseEmitterRequest;
import com.fan.chat.model.JSONResult;
import com.fan.chat.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Slf4j
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/context")
    public JSONResult<?> context(@RequestBody SseEmitterRequest request) {
        log.info("收到请求：{}", request);
        if (request.getId() == null || request.getContext() == null) {
            return JSONResult.error();
        }
        chatService.context(request);
        return JSONResult.success(request.getId());
    }

    @GetMapping("/sse")
    public SseEmitter sseEmitter(SseEmitterRequest request) {
        log.info("收到请求：{}", request);
        return chatService.sseEmitter(request);
    }
}
